<template>
  <div class="main">
    <div class="leftTree">
      <el-tree :data="treeData" :props="defaultProps" default-expand-all :expand-on-click-node="false" accordion @node-click="handleNodeClick" />
    </div>
    <div class="app-container">
      <div class="contentDiv">
        <div class="clearfix" style="margin-bottom:10px;height:32px">
          <el-button v-permiss="['60203870']" size="small" type="primary" style="float:right;" round @click="showImport">导入</el-button>
          <el-button v-permiss="['60203820']" size="small" type="primary" style="float:right;margin-right:10px" round @click="handleAdd">新增</el-button>
        </div>
        <el-table :key="tableKey" v-loading="listLoading" :data="tableData" fit highlight-current-row default-expand-all style="width: 100%;" size="small" row-key="id">
          <el-empty slot="empty" :image-size="120" description="暂无数据" />
          <el-table-column label="序号" type="index" width="50" align="center" />
          <el-table-column label="存货分类代码" align="center">
            <template slot-scope="{row}">
              <span>{{ row.invCatgCode }}</span>
            </template>
          </el-table-column>
          <el-table-column label="存货分类名称" align="center">
            <template slot-scope="{row}">
              <span>{{ row.invCatgName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否末级" align="center">
            <template slot-scope="{row}">
              <span>{{ row.leaf==1?'是':'否' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center">
            <template slot-scope="{row}">
              <span>{{ row.remarks }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否启用" align="center">
            <template slot-scope="{row}">
              <el-switch v-model="row.isDisable" :active-value="0" :inactive-value="1" @change="updateStatus($event, row)" />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="180">
            <template slot-scope="{row}">
              <el-button v-permiss="['60203830']" type="text" size="small" @click="handleCompile(row)">编辑</el-button>
              <el-button v-permiss="['60203860']" type="text" size="small" @click="handleDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog :close-on-click-modal="false" :title="dialogStatus=='create'?'新增存货分类':'修改存货分类'" :visible.sync="dialogVisible" width="475px">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="110px" style="width: 420px; margin-left:5px;">
        <el-form-item label="存货分类代码" prop="invCatgCode">
          <el-input v-model="form.invCatgCode" placeholder="存货分类代码" />
        </el-form-item>
        <el-form-item label="存货分类名称" prop="invCatgName">
          <el-input v-model="form.invCatgName" placeholder="存货分类名称" />
        </el-form-item>
        <el-form-item label="上级分类" prop="parentId">
          <treeselect v-model="form.parentId" :options="treeData" :normalizer="normalizer" placeholder="选择上级分类" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="备注" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isDisable">
          <el-radio v-model="form.isDisable" :label="0">是</el-radio>
          <el-radio v-model="form.isDisable" :label="1">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave()">确定</el-button>
      </div>
    </el-dialog>
    <import-data ref="importRef" type="invCatg" title="存货分类" />
  </div>
</template>
<script>
import { saveInvCatg, delInvCatg, updateInvCatgDisabled, getItemTree } from '@/api/basedata'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { deepClone } from '@/utils/index'
import importData from './importData'
export default {
  name: 'BaseInvCatg',
  components: { Treeselect, importData },
  data() {
    return {
      tableKey: 0,
      tableData: [],
      treeData: [],
      parentId: '',
      defaultProps: {
        children: 'children',
        label: 'invCatgName'
      },
      total: 0,
      listLoading: true,
      listQuery: {
        parentId: null
      },
      form: {
        invCatgName: '',
        invCatgCode: '',
        invLevel: '',
        parentId: null,
        remarks: '',
        isDisable: 0
      },
      dialogVisible: false,
      dialogStatus: '',
      rules: {
        invCatgName: [
          { required: true, message: '名称不能为空', trigger: 'change' }
        ],
        invCatgCode: [
          { required: true, message: '代码不能为空', trigger: 'change' }
        ],
        invLevel: [
          { required: true, message: '代码级次不能为空', trigger: 'change' }
        ],
        parentId: [
          { required: true, message: '上级分类不能为空', trigger: 'change' }
        ],
        isDisable: [{ required: false }]
      },
      importForm: {
        fileName: ''
      },
      fileRule: [
        { required: true, message: '请选择要导入的文件！', trigger: 'change' }
      ],
      dialogVisible1: false,
      fileList: []
    }
  },
  created() {
    this.getList()
    this.getLeftTree()
  },
  methods: {
    showImport() {
      this.$refs.importRef.option = {
        show: true
      }
    },
    normalizer(node) {
      if (!node.children || (node.children && !node.children.length)) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.invCatgName,
        children: node.children
      }
    },
    handleNodeClick(e) {
      this.listQuery.parentId = e.id === 'root' ? null : ''
      this.parentId = e.id === 'root' ? null : ''
      this.getList()
    },
    getLeftTree() {
      getItemTree({ parentId: null, includeRoot: 0 }).then((res) => {
        this.treeData = res.data.data || []
        this.treeData[0].id = 'root'
      })
    },
    getList() {
      this.listLoading = true
      const obj = this.listQuery
      obj.parentId = this.parentId
      getItemTree(obj).then((res) => {
        this.listLoading = false
        this.tableData = res.data.data || []
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleAdd() {
      this.dialogVisible = true
      this.dialogStatus = 'create'
      this.form.id = ''
      this.form.parentId = this.parentId || null
      this.form.invCatgName = ''
      this.form.invCatgCode = ''
      this.form.isDisable = 0
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCompile(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'update'
      this.form.id = obj.id
      this.form.parentId = obj.parentId || null
      this.form.invCatgName = obj.invCatgName
      this.form.invCatgCode = obj.invCatgCode
      this.form.isDisable = obj.isDisable
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateStatus(e, row) {
      this.changeAvaiable(e, row.id)
    },
    changeAvaiable(isDisable, id) {
      var obj = { isDisable, id }
      updateInvCatgDisabled(obj).then((res) => {
        if (res.data.errorCode === '0') {
          this.$message.success(res.data.msg)
        }
      })
    },
    handleDel(id) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delItem(id)
      })
    },
    handleSave() {
      console.log(this.form)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const obj = deepClone(this.form)
          obj.parentId = obj.parentId === 'root' ? null : obj.parentId
          saveInvCatg(obj).then((res) => {
            if (res.data.errorCode === '0') {
              this.parentId = null
              this.getList()
              this.getLeftTree()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    delItem(id) {
      delInvCatg(id).then((res) => {
        if (res.data.errorCode === '0') {
          this.getList()
          this.getLeftTree()
          this.dialogVisible = false
          this.$message.success(res.data.msg)
        }
      })
    }
  }
}
</script>
<style scoped>
.main {
  position: relative;
  width: 100%;
  height: 100%;
  padding-left: 190px;
}

.leftTree {
  width: 180px;
  height: 100%;
  background-color: #fff;
  position: absolute;
  top: 10px;
  left: 10px;
  border: 1px #eee solid;
  padding: 10px 5px;
}

.tableDiv {
  width: 100%;
}

.fileDiv {
  width: 400px;
  height: 60px;
  border: 1px #d0d0d0 solid;
  line-height: 60px;
  text-align: center;
  border-radius: 6px;
}
.filter-item {
  position: relative;
}
</style>
